package com.quick.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import com.quick.common.core.domain.Dict;
import com.quick.common.utils.DictUtils;
import com.quick.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quick.system.mapper.SysDictDataMapper;
import com.quick.system.domain.SysDictData;
import com.quick.system.service.SysDictDataService;
import org.springframework.util.CollectionUtils;

/**
 * 字典数据Service业务层处理
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Service
public class SysDictDataServiceImpl implements SysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询字典数据
     *
     * @param dictCode 字典数据主键
     * @return 字典数据
     */
    @Override
    public SysDictData selectSysDictDataByDictCode(Long dictCode) {
        return sysDictDataMapper.selectSysDictDataByDictCode(dictCode);
    }

    /**
     * 查询字典数据列表
     *
     * @param sysDictData 字典数据
     * @return 字典数据
     */
    @Override
    public List<SysDictData> selectSysDictDataList(SysDictData sysDictData) {
        return sysDictDataMapper.selectSysDictDataList(sysDictData);
    }

    /**
     * 新增字典数据
     *
     * @param sysDictData 字典数据
     * @return 结果
     */
    @Override
    public int insertSysDictData(SysDictData sysDictData) {
        sysDictData.setCreateTime(new Date());
        sysDictData.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return sysDictDataMapper.insertSysDictData(sysDictData);
    }

    /**
     * 修改字典数据
     *
     * @param sysDictData 字典数据
     * @return 结果
     */
    @Override
    public int updateSysDictData(SysDictData sysDictData) {
        sysDictData.setUpdateTime(new Date());
        sysDictData.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return sysDictDataMapper.updateSysDictData(sysDictData);
    }

    /**
     * 批量删除字典数据
     *
     * @param dictCodes 需要删除的字典数据主键
     * @return 结果
     */
    @Override
    public int deleteSysDictDataByDictCodes(Long[] dictCodes) {
        return sysDictDataMapper.deleteSysDictDataByDictCodes(dictCodes);
    }

    /**
     * 删除字典数据信息
     *
     * @param dictCode 字典数据主键
     * @return 结果
     */
    @Override
    public int deleteSysDictDataByDictCode(Long dictCode) {
        return sysDictDataMapper.deleteSysDictDataByDictCode(dictCode);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<Dict> selectDictDataByType(String dictType) {
        List<Dict> dictList = DictUtils.getDictCache(dictType);
        if (!CollectionUtils.isEmpty(dictList)) {
            return dictList;
        }
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByType(dictType);
        if (!CollectionUtils.isEmpty(sysDictDataList)) {
            dictList = new ArrayList<>();
            for (SysDictData sysDictData : sysDictDataList) {
                Dict dict = new Dict();
                dict.setDictValue(sysDictData.getDictValue());
                dict.setDictLabel(sysDictData.getDictLabel());
                dictList.add(dict);
            }
            DictUtils.setDictCache(dictType, dictList);
            return dictList;
        }
        return new ArrayList<>();
    }


}
